from info import redis_store, db
from . import index_blue
import logging
from info.utils.response_code import RET
from flask import current_app, render_template, jsonify, g
from info.models import User, News, Category

@index_blue.route('/', methods=["GET","POST"])
def show_index():
    # 3.查询热门新闻,根据点击量,查询前十条新闻
    try:
        news = News.query.order_by(db.desc(News.clicks)).limit(10).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")
    # 4.将新闻对象列表转成,字典列表
    news_list = []
    for item in news:
        news_list.append(item.to_dict())

    #3.拼接用户数据,渲染页面
    data = {
        #如果user有值返回左边的内容, 否则返回右边的值
        # "user_info": g.user.to_dict() if g.user else "",
        "new_list": news_list
    }
    return render_template("news/index.html", data=data)





#处理网站logo
@index_blue.route('/favicon.ico')
def get_web_logo():
    return current_app.send_static_file('news/favicon.ico')